import Vue from "vue"
import VueRouter from 'vue-router'

const Home = () => import('pages/home/Home')
const Category = () => import('pages/category/Category')
const Cart = () => import('pages/cart/Cart')
const User = () => import('pages/user/User')
const Login = () => import('pages/login/Login')
const Register = () => import('pages/register/Register')
const Profile = () => import('pages/user/childCom/Profile')
const Product = () => import('pages/product/Product')
const Detail = () => import('pages/detail/Detail')
const Proxy = () => import('pages/login/Proxy')

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: Home,
    meta: {
      index: 1
    }
  },
  {
    path: '/category',
    name: 'Category',
    component: Category,
    meta: {
      index: 2
    }
  },
  {
    path: '/cart',
    name: 'Cart',
    component: Cart,
    meta: {
      index: 1,
      auth: true
    }
  },
  {
    path: '/user',
    name: 'User',
    component: User,
    meta: {
      index: 2,
      auth: true
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: {
      index: 2
    }
  },
  {
    path: '/proxy',
    name: 'Proxy',
    component: Proxy
  },
  {
    path: '/register',
    name: 'Register',
    component: Register
  },
  {
    path: '/profile',
    name: 'Profile',
    component: Profile
  },
  {
    path: '/product/:id',
    name: 'Product',
    component: Product
  },
  {
    path: '/detail/:id',
    name: 'Detail',
    component: Detail,
    meta: {
      index: 2
    }
  }

]

const router = new VueRouter({
  routes,
  mode: 'history'
})

// 路由拦截
router.beforeEach((to, from, next) => {
  // 
  if (to.meta.auth) {
    // 获取用户的token 或者用户信息
    const token = window.sessionStorage.getItem('token')

    if (token) {

      next()
    } else {

      next({ path: '/login' })
    }

  } else {
    next()
  }


})



export default router